/**
 * 制作者：九哥
 * 注册数据对接
 * 时间：2019-03-06
 * */





/**
 * 引入缓存设置文件 cache.js
 * */
document.write("<script type='text/javascript' src='/h5/js/cache.js'></script>");





//var app_access = 'qSoiKwpWVge4TkaryH6MKvOdceGt7ZMaF20g8H0cnXI';





/**
 * 发送验证码
 * */
$('#IgetCode').on('click',function(){
    var phone = $('.regPhoneText').val();
    var reg = /^1[34578]\d{9}$/; //验证规则
    if (!reg.test(phone)) {
        getErrMsg('手机号码未填或格式错误！',''); return;
    }
    $.ajax({
        type:'post',
        url:getDomain() + 'yzm',
        data:{ app_access:app_access,phone:phone },
        success:function(res){
            //console.log(res); return
            if (res.code==-6) {
                $('#IgetCode').html('已存在');
            }
            if (res.code!=0) {
                getErrMsg(res.msg,''); return;
            }
            getErrMsg('发送成功！','');
        },
        error:function(){
            getErrMsg('操作错误！','');
        }
    });
});

/**
 * 注册，手机号码、密码
 * */
$('#toRegist').on('click',function(){
    var mobile = $('#mobile').val();
    var yzm = $('#yzm').val();
    var pwd = $('#pwd').val();
    var userNotice = $('#userNotice').val();
    var regMobile = /^1[3453678]\d{9}$/; //手机号码验证规则
    if (!regMobile.test(mobile)) {
        getErrMsg('手机号码未填或格式错误！',''); return;
    }
    if (yzm=='' || pwd=='') {
        getErrMsg('验证码或密码未填！',''); return;
    }
    if (userNotice!=1) {
        getErrMsg('“用户注册须知”未选择！',''); return;
    }
    var data = {
        app_access:app_access,
        phone:mobile,
        yzm:yzm,
        pwd:pwd
    };
    $.ajax({
        type:'post',
        url:getDomain() + 'regist',
        data:data,
        success:function(res){
            //console.log(res); return;
            if (res.code!=0) {
                getErrMsg(res.msg,''); return;
            }
            var type = res.data.type;
            var cate = res.data.cate;
            var userInfo = {
                token:res.data.token,
                type:type,
                cate:cate,
                userNotice:1
            };
            cacheSet('userInfo',userInfo);
            //getErrMsg('登录成功，将跳转到首页面！');
            //toView(type,cate); return;
            //注册成功，记录用户须知到缓存
            var viewLink = toView(type,cate);
            getErrMsg('注册成功！',viewLink); return;
        },
        error:function(){
            getErrMsg('操作错误！','');
        }
    });
});

/**
 * 登录，手机号码、密码
 * */
$('#toLogin').on('click',function(){
    var mobile = $('.regCode').val();
    var pwd = $('.regPhoneText').val();
    var regMobile = /^1[34578]\d{9}$/; //手机号码验证规则
    var regPwd = /^[0-9a-zA-Z_]{5,20}$/; //密码验证规则
    if (!regMobile.test(mobile)) {
        getErrMsg('手机号码未填或格式错误！',''); return;
    }
    if (!regPwd.test(pwd)) {
        getErrMsg('密码未填或格式错误！',''); return;
    }
    var data = {
        app_access:app_access,
        phone:mobile,
        pwd:pwd
    };
    $.ajax({
        type:'post',
        url:getDomain() + 'login',
        data:data,
        success:function(res){
            //console.log(res); return;
            if (res.code!=0) {
                getErrMsg(res.msg,''); return;
            }
            //存储用户信息
            var type = res.data.type;
            var cate = res.data.cate;
            var userInfo = {
                'token':res.data.token,
                'type':type,
                'cate':cate
            };
            cacheSet('userInfo',userInfo);
            MC.cookie('token',userInfo.token);
            //getErrMsg('登录成功，将跳转到首页面！');
            //toView(type,cate); return;
            var viewLink = toView(type,cate);
            getErrMsg('登录成功！',viewLink); return;
        },
        error:function(){
            getErrMsg('操作错误！','');
        }
    });
});

/**
 * 密码重置，手机号码、密码
 * */
$('#setPwd').on('click',function(){
    var mobile = $('#phone').val();
    var pwd = $('#pwd').val();
    var yzm = $('#yzm').val();
    var regMobile = /^1[34578]\d{9}$/; //手机号码验证规则
    var regPwd = /^[0-9a-zA-Z_]{5,20}$/; //密码验证规则
    //if (!mobile || !pwd || !yzm) {
    //    getErrMsg('手机号码、密码或验证码未填！',''); return;
    //}
    if (!regMobile.test(mobile)) {
        getErrMsg('手机号码未填或格式错误！',''); return;
    }
    if (!regPwd.test(pwd)) {
        getErrMsg('密码未填或格式错误！',''); return;
    }
    var data = {
        app_access:app_access,
        phone:mobile,
        pwd:pwd,
        yzm:yzm
    };
    $.ajax({
        type:'post',
        url:getDomain() + 'pwd',
        data:data,
        success:function(res){
            //console.log(res); return;
            if (res.code!=0) {
                getErrMsg(res.msg,''); return;
            }
            cacheClear();
            //跳转到登录页面
            getErrMsg('重置成功！','/h5/login/login.html');
            //window.location.href = 'login.html';
            return;
        },
        error:function(){
            getErrMsg('操作错误！','');
        }
    });
});

/**
 * 主体身份选择
 * */
$('.identityItem').on('click',function(){
    var type = this.getAttribute('data-id');
    $('.identityItem').removeClass('indentityClick');
    $(this).addClass('indentityClick');
    $('#indentity')[0].value = type;
});

/**
 * 主体身份设置
 * */
$('#indentityNext').on('click',function(){
    var type = $('#indentity').val();
    var cate;
    var introducer = $('#introducer').val();
    var userInfo = cacheGet('userInfo'); //获取用户信息
    if (type==0) { getErrMsg('主体身份未设置！',''); return; }
    if (userInfo==null || userInfo.token=='undefined') {
        getErrMsg('没有登录！',''); return;
    }
    //企业类型判断
    if (type==2) { cate = 2; }
    else if (type==3) { cate = 1; }
    else if (type==4) { cate = 3; }
    else { cate = userInfo.cate; } //个人类型
    //存储到本地
    var dataCache = {
        app_access:app_access,
        token:userInfo.token,
        type:type,
        cate:cate
    };
    cacheSet('userInfo',dataCache);
    //同步到线上
    var data = {
        app_access:app_access,
        token:userInfo.token,
        type:type,
        cate:cate,
        introducer:introducer
    };
    $.ajax({
        type:'post',
        url:getDomain() + 'indentity',
        data:data,
        success:function(res){
            //console.log(res); return;
            if (res.code!=0) {
                getErrMsg(res.msg,''); return;
            }
            //toView(type,cate); return;
            var viewLink = toView(type,0);
            getErrMsg('设置成功！',viewLink); return;
        },
        error:function(){
            getErrMsg('操作错误！','');
        }
    });
});

/**
 * 主体类型，页面跳转
 * */
function toView(type,cate){
    var toView;
    if (type==1) { //员工
        if (cate) {
            //主体、资料都有，跳转到首页面
            //window.location.href = '../index.html';
            toView = '/h5/index.html?type=regist';
        } else {
            //主体有、资料无，跳转到职员选择页面
            //window.location.href = 'staffType.html';
            //toView = '/h5/login/staffType.html';
            toView = '/h5/login/staffList.html?type=regist';
        }
    //} else if (type==2 || type==4) { //仅公司、公司含工厂
    //    if (cate) {
    //        //主体、资料都有，跳转到首页面
    //        //window.location.href = '../index.html';
    //        toView = '/h5/index.html';
    //    } else {
    //        //主体有、资料无，跳转到公司资料页面
    //        //window.location.href = 'company.html';
    //        //toView = '/h5/login/company.html';
    //        toView = '/h5/login/comList.html';
    //    }
    } else if (type==2) { //仅公司
        if (cate) {
            //主体、资料都有，跳转到首页面
            //window.location.href = '../index.html';
            toView = '/h5/index.html?type=regist';
        } else {
            //主体有、资料无，跳转到公司资料页面
            //window.location.href = 'company.html';
            //toView = '/h5/login/company.html';
            toView = '/h5/login/comList.html?type=regist';
        }
    } else if (type==4) { //公司含工厂
        if (cate) {
            //主体、资料都有，跳转到首页面
            //window.location.href = '../index.html';
            toView = '/h5/index.html?type=regist';
        } else {
            //主体有、资料无，跳转到公司资料页面
            //window.location.href = 'company.html';
            //toView = '/h5/login/company.html';
            toView = '/h5/login/comList.html?type=regist';
        }
    } else if (type==3) { //工厂
        if (cate) {
            //主体、资料都有，跳转到首页面
            //window.location.href = '../index.html';
            toView = '/h5/index.html?type=regist';
        } else {
            //主体有、资料无，跳转到工厂类型页面
            //window.location.href = 'facType.html';
            //toView = '/h5/login/facType.html';
            toView = '/h5/login/facList.html?type=regist';
        }
    } else {
        //跳转到主体类型页面
        //window.location.href = 'indentity.html';
        toView = '/h5/login/indentity.html?type=regist';
    }
    return toView;
}





/**
 * 地址前缀
 * */
function getDomain(){
    return '/api/member/';
}

///**
// * 测试存储
// * */
//$('#toRegist').on('click',function(){
//    cacheSet('test','tttt');
//    getErrMsg('测试存储');
//});
//$('#getCache').on('click',function(){
//    getErrMsg('测试：'+cacheGet('test'));
//});


///**
// * -------------------------
// * 本地缓存
// * -------------------------
// * */
//
///**
// * 本地缓存，获取
// * */
//function cacheGet(key){
//    //return localStorage.getItem(key);
//    return JSON.parse(localStorage.getItem(key));
//}
//
///**
// * 本地缓存，新增
// * */
//function cacheSet(key,val){
//    //var cache = cacheGet(key);
//    //if (cache!='undefined') { getErrMsg('已存在！'); }
//    //localStorage.setItem(key,val);
//    //为防止没有设置值获取后出现object类型 转化出错,提前进行设置空字符串
//    localStorage.setItem(key, "");
//    localStorage.setItem(key, JSON.stringify(val));
//}
//
///**
// * 本地缓存，删除
// * */
//function cacheDel(key){
//    localStorage.remove(key);
//}
//
///**
// * 本地缓存，删除
// * */
//function cacheClear(){
//    localStorage.clear();
//}





/**
 * 错误提示框
 * */
function getErrMsg(msg,url){
    var h = '';
    h += '<span>'+msg+'</span>';
    h += '<script>';
    h += 'setTimeout("setErrClose()",1000*2); function setErrClose(){ $("#errMsg").html("").hide(); }';
    if (url!='') {
        h += 'setTimeout("setJump()",1000*2); function setJump(){ window.location.href=\''+url+'\'; }';
    }
    h += '</script>';
    $('#errMsg').html(h).show();
}